package cn.itcast.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.common.utils.Page;
import cn.itcast.core.bean.BaseDict;
import cn.itcast.core.bean.BaseDictExample;
import cn.itcast.core.bean.BaseDictExample.Criteria;
import cn.itcast.core.bean.Customer;
import cn.itcast.core.bean.QueryVo;
import cn.itcast.core.dao.BaseDictMapper;
import cn.itcast.core.bean.CustomerExample;
import cn.itcast.core.dao.CustomerMapper;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private BaseDictMapper baseDictMapper;
	
	@Autowired
	private CustomerMapper customerMapper;
	
	@Override 
	public List<BaseDict> findBaseDictByTypeCode(String typeCode) {
		// 2:查询条件需要使用BaseDictExample,所以创建
		BaseDictExample example = new BaseDictExample();
		// 3:创建查询条件example.createCriteria()
		Criteria createCriteria = example.createCriteria();
		// 4:根据typecode进行查询
		// 调用方式:方法开头使用and 后边跟上 属性
		createCriteria.andDictTypeCodeEqualTo(typeCode);
		// 1:根据逆向工程查询
		return baseDictMapper.selectByExample(example);
	}

	@Override
	public Page<Customer> findCustomerByQueryVo(QueryVo vo) {
		Page<Customer> page = new Page<>();
		CustomerExample example = new CustomerExample();
		// 计算后台分页  sql语句
		example.setStartRow((vo.getPage() - 1) * vo.getSize());// (page - 1) * size
		example.setSize(vo.getSize());
		// 拼装查询条件
		cn.itcast.core.bean.CustomerExample.Criteria createCriteria = example.createCriteria();
		// 客户名称
		if(null != vo.getCustName() && !"".equals(vo.getCustName())){
			createCriteria.andCustNameLike("%"+vo.getCustName()+"%");
		}
		// 客户来源
		if(null != vo.getCustSource() && !"".equals(vo.getCustSource())){
			createCriteria.andCustSourceEqualTo(vo.getCustSource());
		}
		// 所属行业 
		if(null != vo.getCustIndustry() && !"".equals(vo.getCustIndustry())){
			createCriteria.andCustIndustryEqualTo(vo.getCustIndustry());
		}
		// 客户级别
		if(null != vo.getCustLevel() && !"".equals(vo.getCustLevel())){
			createCriteria.andCustLevelEqualTo(vo.getCustLevel());
		}
		List<Customer> list = customerMapper.selectByExample(example);
		// 前台需要的分页
		page.setPage(vo.getPage());
		page.setSize(vo.getSize());
		page.setRows(list);
		// 总数
		page.setTotal(customerMapper.countByExample(example));
		return page;
	}

	@Override
	public Customer findCustomerById(Long id) {
		return customerMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateCustomer(Customer customer) {
		customerMapper.updateByPrimaryKeySelective(customer);
	}

	@Override
	public void deleteCustomerById(Long id) {
		customerMapper.deleteByPrimaryKey(id);
	}

}
